#include "function.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QImage>
#include <cstring>
#include <cmath>

#define PI 3.14159

//构造函数
Function::Function(QImage image) {
    initialImage = image;
    lastImage = image;
    finalImage = image;
}

//默认构造函数
Function::Function() {

}

//拷贝函数
void Function::copyPicture(QImage image) {
    initialImage = image;
    lastImage = image;
    finalImage = image;
}

//获取原始图片
QImage Function::getInitialImage() {
    return initialImage;
}

//获取上一张图片
QImage Function::getLastImage() {
    return lastImage;
}

//获取最新修改的图片
QImage Function::getFinalImage() {
    return finalImage;
}

//修改上一步修改的图片
void Function::setLastImage(QImage image) {
    lastImage = image;
}

//修改最新图片
void Function::setFinalImage(QImage image) {
    finalImage = image;
}

//图形变换————放大
QImage Function::expandPicture(int a) {
    lastImage = finalImage;

    double rate = a / 100;

    //原图像的高宽
    int pre_width = finalImage.width();
    int pre_height = finalImage.height();

    //扩大后图像的高宽
    int aft_width = pre_width * rate;
    int aft_height = pre_height * rate;

    //创建一个新的图像
    QImage* newImage = new QImage(aft_width, aft_height, QImage::Format_ARGB32);

    unsigned char* pre_pixel = NULL;
    unsigned char* aft_pixel = NULL;

    int x = 0, y = 0;

    for(int i = 0; i < aft_height; i++) {
        for(int j = 0; j < aft_width; j++) {
            //由左下到右上依次遍历每一个像素点，求出其对应原图的像素点坐标
            x = (int)(j / rate + 0.5);
            y = (int)(i / rate + 0.5);

            if(x == pre_width) {
                x--;
            }
            if(y == pre_height) {
                y--;
            }

            pre_pixel = finalImage.bits() + y * pre_width * 4  + x * 4;
            aft_pixel = newImage->bits() + i * aft_width * 4 + j * 4;

            //调用memcpy函数复制像素数据
            if(x >= 0 && x < pre_width && y >= 0 && y < pre_height) {
                memcpy(aft_pixel, pre_pixel, 4);
            }
        }
    }

    finalImage = *newImage;
    delete newImage;
    return finalImage;
}

//图形变换————缩小
QImage Function::shrinkPicture(int a) {
    lastImage = finalImage;

    double rate = a / 100;

    //原图像的高宽
    int pre_width = finalImage.width();
    int pre_height = finalImage.height();

    //缩小后图像的高宽
    int aft_width = pre_width * rate;
    int aft_height = pre_height * rate;

    //创建一个新的图像
    QImage* newImage = new QImage(aft_width, aft_height, QImage::Format_ARGB32);

    unsigned char* pre_pixel = NULL;
    unsigned char* aft_pixel = NULL;

    int x = 0, y = 0;

    for(int i = 0; i < aft_height; i++) {
        for(int j = 0; j < aft_width; j++) {
            //由左下到右上依次遍历每一个像素点，求出其对应原图的像素点坐标
            x = (int)(j * rate + 0.5);
            y = (int)(i * rate + 0.5);

            if(x == pre_width) {
                x--;
            }
            if(y == pre_height) {
                y--;
            }

            pre_pixel = finalImage.bits() + y * pre_width * 4  + x * 4;
            aft_pixel = newImage->bits() + i * aft_width * 4 + j * 4;

            //调用memcpy函数复制像素数据
            if(x >= 0 && x < pre_width && y >= 0 && y < pre_height) {
                memcpy(aft_pixel, pre_pixel, 4);
            }
        }
    }

    finalImage = *newImage;
    delete newImage;
    return finalImage;
}

//返回一个最大值
double max(double x, double y) {
    if(x>y) return x;
    else return y;
}

//图形变换————旋转
QImage Function::revolvePicture(double a) {
    lastImage = finalImage;

    //原图像的高宽
    int pre_width = finalImage.width();
    int pre_height = finalImage.height();

    double angle = PI * a / 180;

    double Sin = sin(angle);
    double Cos = cos(angle);

    //计算旋转后图像的高宽
    int aft_width = pre_width * fabs(Cos) + pre_height * fabs(Sin);
    int aft_height = pre_width * fabs(Sin) + pre_height *fabs(Cos);

    //创建一个新的图像
    QImage* newImage = new QImage(aft_width, aft_height, QImage::Format_ARGB32);

    //计算要用到的数据
    double dx = -0.5 * aft_width * Cos - 0.5 * aft_height * Sin + 0.5 * pre_width;
    double dy =  0.5 * aft_width * Sin - 0.5 * aft_height * Cos + 0.5 * pre_height;

    unsigned char* pre_pixel = NULL;
    unsigned char* aft_pixel = NULL;
    int x = 0;
    int y = 0;

    for(int i = 0; i < aft_height; i++) {
        for(int j = 0; j < aft_width; j++) {
            //用旋转公式计算出旋转后图像的点对应的原图上的点
            x = (int)(j * Cos + i * Sin + dx);
            y = (int)(-j * Sin + i * Cos + dy);

            if(x == pre_width) {
                x--;
            }
            if(y == pre_height) {
                y--;
            }

            pre_pixel = finalImage.bits() + y * pre_width * 4  + x * 4;
            aft_pixel = newImage->bits() + i * aft_width * 4 + j * 4;

            //调用memcpy函数复制像素数据
            if(x >= 0 && x < pre_width && y >= 0 && y < pre_height) {
                memcpy(aft_pixel, pre_pixel, 4);
            }
        }
    }

    finalImage = *newImage;
    delete newImage;
    return finalImage;
}

//方框滤波
QImage Function::squarePicture() {

}

//中值滤波
QImage Function::midPicture() {

}

//高斯滤波
QImage Function::gaosiPicture() {

}

//双边滤波
QImage Function::sidesPicture() {

}

//均值滤波
QImage Function::averagePicture() {

}

//吸管
QImage Function::strawPicture() {

}

//油漆桶
QImage Function::bucketPicture() {

}

//画笔
QImage Function::paintPicture() {

}

//裁剪
QImage Function::scissorsPicture() {

}

//拼图
QImage Function::jigsawPicture() {

}

//添加文字
QImage Function::textPicture() {

}

